package com.github.linkoog.model_designer.ide.editer;

import com.github.linkoog.model_designer.ide.editer.common.Callback;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.ui.ToolbarDecorator;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ColumnsTableBuilder {

    private static final Logger LOGGER = Logger.getInstance(ColumnsTableBuilder.class);

    private final Project project;
    private Callback selectCallback;

    private ToolbarDecorator toolbarDecorator;
    private ColumnsJTable columnsJTable;

    public ColumnsTableBuilder(Project project){
        this.project = project;
        initUiPanel();
    }

    private void initUiPanel(){
        columnsJTable = new ColumnsJTable();

        columnsJTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                final JTable table = (JTable) e.getSource();
                int row = table.rowAtPoint(e.getPoint()); //获得行位置
                int col = table.columnAtPoint(e.getPoint()); //获得列位置
                if (selectCallback != null){
                    selectCallback.callback();
                }

                if (row == -1 || col == -1){
                    table.clearSelection();
                    return;
                }

                if (col == 2){
                    //点击行中的编辑列
                    return;
                }
            }
        });

        toolbarDecorator = ToolbarDecorator.createDecorator(columnsJTable);
    }

    public JPanel getMyMainPanel() {
        return toolbarDecorator.createPanel();
    }

    public ToolbarDecorator getToolbarDecorator() {
        return toolbarDecorator;
    }

    public ColumnsJTable getColumnsJTable() {
        return columnsJTable;
    }

    public Callback getSelectCallback() {
        return selectCallback;
    }

    public void setSelectCallback(Callback selectCallback) {
        this.selectCallback = selectCallback;
    }

}
